import { createI18n } from 'vue-i18n';
import { useStore } from "@/stores/store";
import zhHK from './zhHK'
import ptBR from './ptBR'
import { Locale } from "vant";
import vant_zhHK from 'vant/es/locale/lang/zh-HK';
import vant_ptBR from 'vant/es/locale/lang/pt-BR';

const vantLang = {
  'zh-HK': vant_zhHK,
  'pt-BR': vant_ptBR
}

// 默认语言
const language = localStorage.getItem('language') || "zh-HK";

// 通过选项创建 VueI18n 实例
const i18n = createI18n({
  legacy: false,  //Not available in legacy mode 解决报错问题的配置项！！！
  locale: language, // 设置地区
  messages: {
    "zh-HK": zhHK,
    "pt-BR": ptBR
  },
})

// i18n.global.locale.value = 'zh-HK'
localStorage.setItem('language', language)
Locale.use(language, vantLang[language]); // 设置 vant 语言


// zh-CN、zh-HK
function changeLang (lang){
  
  localStorage.setItem('language', lang) // 更新 localStorage language
  
  // 更新 store
  const store = useStore()
  store.changeLang(lang)
  
  i18n.global.locale.value = lang // 更改业务语言
  Locale.use(lang, vantLang[lang]); // 更改 vant 语言
}


export {i18n, changeLang}